package club.framework.web.servlet;

import club.framework.web.base.common.Constants;
import club.framework.web.base.util.ValidateCodeUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;

/**
 * @date 2017/9/27.
 * @Email:sunmch@163.com
 * @author: 念梓
 * des:生成验证码servlet实体类
 */

@WebServlet("/api/validatecode")
public class ValidateCodeServlert extends HttpServlet {

    private static final int VALIDATE_CODE_LENGTH=4;

    private static final int VALIDATE_CODE_WIDTH=200;

    private static final int VALKIDATE_CODE_HEIGHT=80;

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String validateCode = ValidateCodeUtils.getValidateCode(VALIDATE_CODE_LENGTH);
        req.getSession().setAttribute(Constants.VALIDATE_CODE,validateCode);
        OutputStream outputStream = resp.getOutputStream();
        ValidateCodeUtils.outputImage(VALIDATE_CODE_WIDTH,VALKIDATE_CODE_HEIGHT,outputStream,validateCode);
        outputStream.close();
    }
}
